package cn.edu.cdtu.healthclient.activity;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.cdtu.healthclient.connect.ConnectForHealthFile;
import cn.edu.cdtu.healthclient.domain.HealthFileMental;
import cn.edu.cdtu.healthclient.service.HealthService;
import cn.edu.cdtu.healthclient.service.HealthService.HealthBinder;
import cn.edu.cdtu.healthclient.task.ActivityTaskQueue;
import cn.edu.scu.healthclient.activity.R;

public class HealthFileMentalActivity extends Activity {

	/*
	 * 
	 */
	private ActivityTaskQueue taskQueue;
	public ActivityTaskQueue getTaskQueue(){
		return taskQueue;
	}
	/*
	 * 
	 */
	private HealthBinder binder;
	public HealthBinder getBinder(){
		return binder;
	}
	/*
	 * 
	 */
	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			binder = null;
			taskQueue.shutdownTaskQueue();
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			binder = (HealthBinder)service;
			if(!taskQueue.isRunning()){
				taskQueue.start();
			}
		}
	};
	/*
	 * 
	 */
	private final Intent serviceIntent = new Intent(HealthService.ACTION);

	private TextView tv_anxiety;
	private TextView tv_hyster;
	private TextView tv_schoizophrenia;
	private TextView tv_disorder;
	private TextView tv_depression;
	private TextView tv_nervous;
	private TextView tv_confabulation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		startService(serviceIntent);
		taskQueue = new ActivityTaskQueue();
		//    	this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		//				WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_file_mental);
		taskQueue.addTask(new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HealthFileMentalAsync fileMentalAsync = new HealthFileMentalAsync();
				fileMentalAsync.execute();
			}
		}));
	}

	/* (non-Javadoc)
	 * <p>Title: onStart</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		bindService(serviceIntent, 
				serviceConnection, Service.BIND_AUTO_CREATE);
		super.onStart();
	}

	/* (non-Javadoc)
	 * <p>Title: onStop</p> 
	 * <p>Description: </p>  
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unbindService(serviceConnection);
	}

	public void initTextView() {
		tv_anxiety = (TextView) findViewById(R.id.anxiety);
		tv_hyster = (TextView) findViewById(R.id.hyster);
		tv_schoizophrenia = (TextView) findViewById(R.id.schoizophrenia);
		tv_disorder = (TextView) findViewById(R.id.disorder);
		tv_depression = (TextView) findViewById(R.id.depression);
		tv_nervous = (TextView) findViewById(R.id.nervous);
		tv_confabulation = (TextView) findViewById(R.id.confabulation);


	}

	public void writeTextView(HealthFileMental mental) {

		tv_anxiety.setText(mental.getAnxiety());
		tv_hyster.setText(mental.getHyster());
		tv_schoizophrenia.setText(mental.getSchoizophrenia());
		tv_disorder.setText(mental.getDepression());
		tv_depression.setText(mental.getDepression());
		tv_nervous.setText(mental.getNervous());
		tv_confabulation.setText(mental.getConfabulation());

	}

	private class HealthFileMentalAsync extends
	AsyncTask<Integer, String, HealthFileMental> {

		@Override
		protected HealthFileMental doInBackground(Integer... arg0) {
			// TODO Auto-generated method stub
			return new ConnectForHealthFile().getHealthFileMental(
					binder.getHttpClient());
		}

		@Override
		protected void onPostExecute(HealthFileMental result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result!=null){
				initTextView();
				writeTextView(result);
			}else{
				Toast.makeText(HealthFileMentalActivity.this, "抱歉，你没进行过心里测试",
						Toast.LENGTH_LONG).show();
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

	}
}
